package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysCoupon;
import com.ruoyi.project.system.domain.SysOrderEvaluate;
import com.ruoyi.project.system.domain.SysReceiveCoupon;
import com.ruoyi.project.system.domain.SysServiceOrder;
import com.ruoyi.project.system.service.ISysCouponService;
import com.ruoyi.project.system.service.ISysOrderEvaluateService;
import com.ruoyi.project.system.service.ISysReceiveCouponService;
import com.ruoyi.project.system.service.ISysServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mobileUser/")
@Api(tags = "创建服务订单", description = "服务订单")
public class PayController extends BaseController {

    @Autowired
    private ISysServiceOrderService sysServiceOrderService;

    @Autowired
    private ISysCouponService sysCouponService;

    @Autowired
    public RedisTemplate redisTemplate;
    @Autowired
    private ISysOrderEvaluateService orderEvaluateService;

    @Autowired
    private ISysReceiveCouponService sysReceiveCouponService;

    @RequestMapping(value = "/selectCouponList", method = RequestMethod.POST)
    @SignatureValidation
    public AjaxResult getUserCoupons(@RequestBody(required = false) SysCoupon sysCoupon) {
        if (null == sysCoupon) {
            return AjaxResult.error("参数错误");
        }
        if (null == sysCoupon.getTypeId()) {
            return AjaxResult.error("分类id为空");
        }
        if (null == sysCoupon.getMoney()) {
            return AjaxResult.error("订单价格必传");
        }
        sysCoupon.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysCoupon> list = sysCouponService.selectUseList(sysCoupon);
        return AjaxResult.success(list);
    }

    @RepeatSubmit
    @SignatureValidation
    @RequestMapping(value = "/createOrderService", method = RequestMethod.POST)
    public AjaxResult createOrderService(@RequestBody(required = false) SysServiceOrder sysServiceOrder) {
        return sysServiceOrderService.createOrderService(sysServiceOrder);
    }

    @RequestMapping(value = "/payOrderService", method = RequestMethod.POST)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult payOrderService(@RequestBody SysServiceOrder sysServiceOrder) {
        return sysServiceOrderService.payOrderService(sysServiceOrder);
    }

    @ApiOperation("用户查询订单")
    @RequestMapping(value = "/sellectMyOrderList", method = RequestMethod.GET)
    @SignatureValidation
    public TableDataInfo sellectMyOrderList(SysServiceOrder sysServiceOrder) {
        startPage();
        sysServiceOrder.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysServiceOrder> serviceOrderList = sysServiceOrderService.sellectMyOrderList(sysServiceOrder);
        for (int i = 0; i < serviceOrderList.size(); i++) {
            String orderCode = serviceOrderList.get(i).getOrderCode();
            SysOrderEvaluate sysOrderEvaluate = new SysOrderEvaluate();
            sysOrderEvaluate.setOrderId(orderCode);
            sysOrderEvaluate.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
            List<SysOrderEvaluate> evaluates = orderEvaluateService.selectSysOrderEvaluateList(sysOrderEvaluate);
            if (evaluates == null) {
                serviceOrderList.get(i).setEvaluationStatus(0);
            } else {
                serviceOrderList.get(i).setEvaluationStatus(1);
            }
        }
        return getDataTable(serviceOrderList);
    }

    /**
     * 没有支付直接取消订单
     *
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/cancelServiceOrder", method = RequestMethod.POST)
    @SignatureValidation
    public AjaxResult cancelServiceOrder(@RequestBody SysServiceOrder sysServiceOrder) {
        SysServiceOrder order = sysServiceOrderService.selectSysServiceOrderById(sysServiceOrder.getOrderId());
        if (null == order) {
            return AjaxResult.error("订单id错误!");
        }
        if ("4".equals(order.getOrderStatus())) {
            return AjaxResult.error("订单超时已取消");
        }
        if (!"0".equals(order.getOrderStatus())) {
            return AjaxResult.error("订单状态错误!");
        }
        order.setOrderStatus("4");
        sysServiceOrderService.updateSysServiceOrder(order);
        //取消订单时判断优惠券是否使用优惠券，如果使用则把优惠券退回给用户
        if(null!=sysServiceOrder.getCouponId()){
            SysReceiveCoupon sysReceiveCoupon=new SysReceiveCoupon();
            sysReceiveCoupon.setCouponId(sysServiceOrder.getCouponId());
            sysReceiveCoupon.setMobileUserId(sysServiceOrder.getMobileUserId());
            sysReceiveCoupon.setUsed("1");
            List<SysReceiveCoupon> list=sysReceiveCouponService.selectSysReceiveCouponList(sysReceiveCoupon);
            if(!list.isEmpty()){
                SysReceiveCoupon receiveCoupon=list.get(0);
                receiveCoupon.setUsed("0");
                sysReceiveCouponService.updateSysReceiveCoupon(receiveCoupon);
            }
        }
        return AjaxResult.success("操作成功!");
    }


    /**
     * 申请取消服务订单
     *
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/applyServiceOrder", method = RequestMethod.POST)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult applyServiceOrder(@RequestBody SysServiceOrder sysServiceOrder) {
        SysServiceOrder order = sysServiceOrderService.selectSysServiceOrderById(sysServiceOrder.getOrderId());
        if (null == order) {
            return AjaxResult.error("订单id错误!");
        }
        if (null == sysServiceOrder.getReason()) {
            return AjaxResult.error("原因不能为空");
        }
        if ("1".equals(order.getOrderStatus()) || "2".equals(order.getOrderStatus())) {
            order.setCancelStatus("0");
            order.setReason(sysServiceOrder.getReason());
            sysServiceOrderService.updateSysServiceOrder(order);
            return AjaxResult.success("提交申请成功");
        }
        return AjaxResult.error("订单状态错误!");
    }

    /**
     * 用户确认完成服务
     *
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
    @RepeatSubmit
    public AjaxResult confirmOrder(@RequestBody SysServiceOrder sysServiceOrder) {
        return sysServiceOrderService.confirmOrder(sysServiceOrder);
    }

}
